﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyFeer.Widget.Grid.Cell;
using MyFeer.Lib.common;

namespace MyFeer.Widget.Grid
{
    public class CellSelectEventListener
    {
        private GridViewer gridViewer;
        private Boolean isPressMouseLeftButton = false;
        private CellModel selectCellModel;
        private IntegerRect selectCellArea;
        public CellSelectEventListener(GridViewer gridViewer)
        {
            this.gridViewer = gridViewer;
            gridViewer.MouseLeftButtonDown += new MouseButtonEventHandler(GridViewer_MouseLeftButtonDown);
            gridViewer.MouseMove += new MouseEventHandler(GridViewer_MouseMove);
            gridViewer.MouseLeftButtonUp += new MouseButtonEventHandler(GridViewer_MouseLeftButtonUp);
        }

        void GridViewer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isPressMouseLeftButton = false;
            selectCellModel = null;
        }


        void GridViewer_MouseMove(object sender, MouseEventArgs e)
        {
            if (isPressMouseLeftButton && selectCellModel!=null)
            {
                Point pos = e.GetPosition(this.gridViewer.GridCanvas);
                CellModel cellModel = this.gridViewer.getCellModel((int)pos.X, (int)pos.Y);
                if (cellModel == null)
                    return;
                IntegerRect cellRect = this.gridViewer.getCellArea(cellModel.Top, cellModel.Left);
                Border selectCellStyle = GridConst.SelectedCellStyle;
                cellRect=this.selectCellArea.Union(cellRect);
                Canvas.SetLeft(selectCellStyle, cellRect.Left);
                Canvas.SetTop(selectCellStyle, cellRect.Top);
                selectCellStyle.Width = cellRect.Width;
                selectCellStyle.Height = cellRect.Height;
                IntegerRect selectRect = new IntegerRect(cellModel.Left, cellModel.Top, cellModel.Left + 1, cellModel.Top + 1);
                this.gridViewer.SelectRect = selectRect;
            }
        }

        void GridViewer_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isPressMouseLeftButton = true;
            Point pos = e.GetPosition(this.gridViewer.GridCanvas);
            CellModel cellModel = this.gridViewer.getCellModel((int)pos.X, (int)pos.Y);
            if (cellModel == null)
                return;
            selectCellModel = cellModel;
            IntegerRect cellRect = this.gridViewer.getCellArea(cellModel.Top, cellModel.Left);
            selectCellArea = cellRect;
            Border selectCellStyle = GridConst.SelectedCellStyle;
            this.gridViewer.GridCanvas.Children.Remove(selectCellStyle);
            Canvas.SetLeft(selectCellStyle, cellRect.Left);
            Canvas.SetTop(selectCellStyle, cellRect.Top);
            selectCellStyle.Width = cellRect.Width;
            selectCellStyle.Height = cellRect.Height;
            this.gridViewer.GridCanvas.Children.Add(selectCellStyle);
            IntegerRect selectRect = new IntegerRect(cellModel.Left, cellModel.Top, cellModel.Left + 1, cellModel.Top + 1);
            this.gridViewer.SelectRect = selectRect;
        }

    }
}
